
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.id {
/**
 * A globally-unique wavelet identifier.
 */
public class WaveletName {
	public var waveId:WaveId;
	public var waveletId:WaveletId;
	
	/** Constructs a wavelet name for a wave id and wavelet id. */
	public static function of(waveId:Object, waveletId:Object):WaveletName {
		if (waveletId is WaveletId)
			return new WaveletName(WaveId(waveId), WaveletId(waveletId));
		else if(waveletId is String)
			return new WaveletName(WaveId.deserialise(String(waveId)), WaveletId.deserialise(String(waveletId)));
		return null;
	}
	
	/** Private constructor to allow future instance optimisation. */
	function WaveletName(waveId:WaveId, waveletId:WaveletId) {
		this.waveId = waveId;
		this.waveletId = waveletId;
	}
	
	
	public function toString():String {
		return waveId + "/" + waveletId;
	}
	
	public function equals(other:Object):Boolean {
		if (other instanceof WaveletName) {
			var o:WaveletName= WaveletName(other);
			return waveId.equals(o.waveId) && waveletId.equals(o.waveletId);
		}
		return false;
	}
	
	public function compareTo(a:Object):int {
		var o:WaveletName= WaveletName(a);
		return waveId.equals(o.waveId) ? waveletId.compareTo(o.waveletId)
			: waveId.compareTo(o.waveId);
	}
}
}